// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import MyApp from './App'
// 引入elementUI
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 引入store，作为全局存储的东西
import store from './store'
import router from './router'
// npm install --save axios
var axios = require('axios')
axios.defaults.baseURL = 'http://1.15.56.65:8001/bookcar/'
// axios.defaults.baseURL = 'http://localhost:8001/bookcar/'

// 全局注册，之后可在其他组件中通过 this.$axios 发送数据
Vue.prototype.$axios = axios

// 作用是阻止vue 在启动时生成生产提示
Vue.config.productionTip = false

// 引入elementUI
Vue.use(ElementUI)

// 钩子函数，beforeEach，为了在每个路由之前，判断用户名是否存在，如果不存在就让他去登陆
router.beforeEach((to, from, next) => {
  console.log(to.fullPath)
  console.log(to.meta.requireAuth)
  if (to.meta.requireAuth) {
    console.log(store.state.user.username)
    if (store.state.user.username) {
      next()
    } else {
      next({
        path: 'login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
}
)

/* eslint-disable no-new */
export default new Vue({
  el: '#bookcarvueapp',
  router,
  // 引入store，作为全局存储的东西
  store,
  components: { MyApp },
  template: '<MyApp/>'
})
